package com.jay.lang.ref.clazz;

import java.lang.ref.Reference;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;

//弱引用所指向的对象只要进行 GC，就会自动进行回收，get() 返回 null。

public class WeakReferenceCls {
	// -Xmx2m -Xms2m
	public static void main(String[] args) {
		Bean b = new Bean("name", 10);
//		WeakReference<Bean> bean = new WeakReference<Bean>(b);
		WeakReference<Bean> bean = new WeakReference<Bean>(new Bean("name", 10));
		System.gc();
		System.runFinalization();
		System.out.println(bean.get());

		Reference<Bean>[] referent = new WeakReference[100000];
		for (int i = 0; i < referent.length; i++) {
			referent[i] = new WeakReference<Bean>(new Bean("mybean:" + i, 100));
		}
		System.out.println(referent[100].get());
	}
}
